import axios from 'axios'
import {
	Toast
} from 'vant'
import vuex from '../store/index.js'
import qs from 'qs'
const instance = axios.create({
	baseURL: 'http://162.14.114.89:9999/api',
	// baseURL:'/api',
	timeout: 1000
})
//请求拦截器
instance.interceptors.request.use(config => {
	Toast.loading({
		message: '加载中',
		duration: 0
	})
	if (vuex.state.token) {
		config.headers['Authorization'] = `Bearer ${vuex.state.token}`
	}
	return config
}, err => {
	console.log(err)
})
//响应拦截器
instance.interceptors.response.use(config => {
	Toast.clear()
	return config
}, err => {
	console.log(err)
})

let http = {
	get(url, params) {
		return new Promise((resolve, reject) => {
			instance.get(url, {
				params: params
			}).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	},
	post(url, params) {
		return new Promise((resolve, reject) => {
			instance.post(url, qs.stringify(params)).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	}
}
export default http
